package homework;

import sun.security.krb5.internal.PAData;

import javax.sound.midi.Soundbank;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: jhy
 * @time: 2021/6/1 22:23
 */

//对一个url进行解析，解析到：协议(http)  域名(www.sohu.com)  端口(8080) 文件名(index.html)
public class HomeworkDemo3 {
    public static void main(String[] args) {

        String url = "http://www.sohu.com:8080/abc/index.html";
        //思路：分组
        Pattern pattern = Pattern.compile("^([a-zA-Z]+)://([a-zA-Z.]+):(\\d+)[\\w-/]*/([\\w.]+)$");
        Matcher matcher = pattern.matcher(url);
        if(matcher.matches()){  //整体匹配，如果匹配成功，可以通过group(x),获取对应分组的内容
            System.out.println("整体匹配="+matcher.group(0));
            System.out.println("协议="+matcher.group(1));
            System.out.println("域名="+matcher.group(2));
            System.out.println("端口="+matcher.group(3));
            System.out.println("资源文件="+matcher.group(4));
        }
    }
}
